package com.xw.screen.capture.service.impl;

import com.xw.screen.capture.entity.CaptureScreenEnum;
import com.xw.screen.capture.service.ScreenService;
import com.xw.screen.capture.thread.CaptureScreenThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author ：xiongwei
 * @date ：Created in 2020/11/22 0:00
 */
@Slf4j
@Service
public class ScreenServiceImpl implements ScreenService {

    private boolean start = false;

    @Override
    public void start() throws Exception {
        start(10);
    }

    @Override
    public void start(int rate) throws Exception {
        if (start) {
            return;
        }
        start = true;
        while (start) {
            for (int i = 0; i < rate; i++) {
                new CaptureScreenThread(i, rate, CaptureScreenEnum.BASE64_IMAGE).start();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        log.info("截屏任务结束了");
    }

    @Override
    public void startOnce() throws Exception {
//        new CaptureScreenThread(0, 1).start();
//        Thread.sleep(1000);
        new CaptureScreenThread(0, 1, CaptureScreenEnum.BASE64_IMAGE).start();
    }


    @Override
    public void stop() throws Exception {
        start = false;
    }


}
